import Taro, { showModal as ShowModal } from '@tarojs/taro'

export function getEnv() {
    return Taro.getEnv()
}

export function getEnvs() {
    return Taro.ENV_TYPE
}

export function useDidShow(callback: (options?: Taro.getLaunchOptionsSync.LaunchOptions | undefined) => void){
    Taro.useDidShow(callback)
}


export function getLocation(option : Taro.getLocation.Option){
    Taro.getLocation(option)
}

export function chooseLocation(option:Taro.chooseLocation.Option){
    Taro.chooseLocation(option)
}

export function openLocation(option:Taro.openLocation.Option){
    Taro.openLocation(option)
}



export function setStorage<T = any>(key:string, data:T) {
    Taro.setStorage({
        key,
        data
    })
}

export function requestSubscribeMessage(tmplIds:string[]){
    return Taro.requestSubscribeMessage({
        tmplIds
    })
}



export function scanCode(option: Taro.scanCode.Option){
    return Taro.scanCode(option)
}

export function getOpenUserInfo(Option: Taro.getOpenUserInfo.Option){
    Taro.getOpenUserInfo(Option)
}


export async function requestPayment(params:Taro.requestPayment.Option) {    
    try {
        const r = await Taro.requestPayment(params)
        return r.errMsg == 'requestPayment:ok'
    } catch (error) {
        console.log(error)
    }

    return false
}
export function getSystemInfoSync() {
    return Taro.getSystemInfoSync()
}

export function createCanvasContext(id:string) {
    return Taro.createCanvasContext(id)
}

export function getStorageSync<T = any>(key:string) : T{
    return Taro.getStorageSync(key)
}

export async function login(client:string ,params : {[key:string]:any}) {
    if(client == 'wx') {
        const res = await Taro.login(params)
        return res.errMsg == 'login:ok' ? res.code : null
    }else {
        const r = await my.getAuthCode(params)
       return r.authCode ? r.authCode : null
    }
}

export async function checkSession() {
    const res = await Taro.checkSession()
    return res.errMsg == 'checkSession:ok'
}



export function openBusinessView(businessType: 'wxpayScoreEnable' | 'wxpayScoreUse' | 'wxpayScoreDetail' | string, apply_permissions_token:string, success?:(e) => void, fail?: (e) => void) {
    Taro.openBusinessView({
        businessType,
        extraData : {
            apply_permissions_token
        },

        success: (e) => {
            if(success) {
                success(e)
            }
        },

        fail: (e) => {
            if(fail) {
                fail(e)
            }
        }
        
    })
}


export function useRouter() {
    return Taro.useRouter()
}

export function navigateTo(url:string) {
    Taro.navigateTo({
        url
    })
}

export function redirectTo(url:string) {
    Taro.redirectTo({
        url
    })
}

export function navigateBack(option?: Taro.navigateBack.Option) {
    Taro.navigateBack(option)
}


export function showToast(title:string) {
    return Taro.showToast({
        title,
        icon : 'none'
    })
}

export function showLoading(title:string = '') {
    Taro.showLoading({
        title,
        mask : true
    })
}

export function hideLoading() {
    Taro.hideLoading()
}

export function showModal(params: Taro.showModal.Option|string) {
    let option:Taro.showModal.Option = {
        
    }
    if(typeof params == 'string') {
        option.content = params
        option.showCancel = false
    }else {
        option = Object.assign(option, params)
    }

    Taro.showModal(option)
}




export function getMenuButtonBoundingClientRect() {
    return Taro.getMenuButtonBoundingClientRect()
}

export function createSelectorQuery() {
    return Taro.createSelectorQuery()
}